﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class PatientBLL
    {
        private static int patientid = 0;
        HospitalDataContext DB = new HospitalDataContext();
        public List<Patient> SelectAll()
        {
            return DB.Patients.ToList();
        
        }
        public List<Patient> SelectDesc()
        {
            return DB.Patients.OrderByDescending(p => p.PatientID).ToList();
        }
        public List<Patient> SelectNew()
        {
            return DB.Patients.Where(p => p.Status == "in").OrderByDescending(p => p.PatientID).ToList();
        }
        public String Insert(PatientDTO giatri)
        {
            try
            {
                Patient patient = new Patient();
                patient.PatientID = (patientid++).ToString();
                patient.LocalDoctorID = giatri.Localdoctorid;
                patient.NOKID = giatri.Nokid;
                patient.FirstName = giatri.FirstName;
                patient.LastName = giatri.Lastname;
                patient.Address = giatri.Address;
                patient.Sex = giatri.Sex;
                patient.Tel = giatri.Tel;
                patient.DateOfBirth = giatri.Dob;
                patient.MaritalStatus = giatri.Maritalstatus;
                patient.DateRegistered = giatri.Dateregistered;
                patient.Status = giatri.Status;
                DB.Patients.InsertOnSubmit(patient);
                DB.SubmitChanges();
                return "success";
            }
            catch(Exception e)
            {
               return e.Message;
            }
        }
        //ham update
        public string Update(PatientDTO nhap)
        {
            try
            {
                Patient patient = DB.Patients.Where(pa => pa.PatientID==nhap.Patientid).FirstOrDefault();
                patient.LocalDoctorID = nhap.Localdoctorid;
                patient.NOKID = nhap.Nokid;
                patient.FirstName = nhap.FirstName;
                patient.LastName = nhap.Lastname;
                patient.Address = nhap.Address;
                patient.Sex = nhap.Sex;
                patient.Tel = nhap.Tel;
                patient.DateOfBirth = nhap.Dob;
                patient.MaritalStatus = nhap.Maritalstatus;
                patient.DateRegistered = nhap.Dateregistered;
                patient.Status = nhap.Status;
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        //ham search theo ID va Name cua Patient
        public List<Patient> Search(string key)
        {
            return DB.Patients.Where(p => p.PatientID.Contains(key) || p.FirstName.Contains(key) || p.LastName.Contains(key)).ToList();
        }
        //ham Search theo Next Of Kin
        public IQueryable SearchWithNOK(string key)
        {
            return DB.Patients.Join(
                DB.NextOfKins,
                a => a.NOKID,
                b => b.NOKID,
                (a, b) => new
                {
                    PatientID = a.PatientID,
                    FirstName = a.FirstName,
                    LastName = a.LastName,
                    Address = a.Address,
                    Sex = a.Sex,
                    Tel = a.Tel,
                    DateRegistered = a.DateRegistered,
                    Status = a.Status,
                    NOKID = b.NOKID,
                    FullName = b.FullName
                }
                ).Where(p => p.NOKID.Contains(key) || p.FullName.Contains(key)).AsQueryable();
        }
        public IQueryable SearchWithLocalDoctor(string key)
        {
            return DB.Patients.Join(
                DB.LocalDoctors,
                a => a.LocalDoctorID,
                b => b.LocalDoctorID,
                (a, b) => new
                {
                    PatientID = a.PatientID,
                    FirstName = a.FirstName,
                    LastName = a.LastName,
                    Address = a.Address,
                    Sex = a.Sex,
                    Tel = a.Tel,
                    DateRegistered = a.DateRegistered,
                    Status = a.Status,
                    LocalDoctorID = b.LocalDoctorID,
                    FullName = b.FullName
                }
                ).Where(p => p.LocalDoctorID.Contains(key) || p.FullName.Contains(key)).AsQueryable();
        }
    }
}
